global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A39_alternative_weights_gdp.log, replace name(tabl)

capture noi {
	
	* v3
	qui do ${code_dir}/config/tabletools.do

	estimates clear

	foreach window in f1970t1989 10yr from1970 from1970_pauto95 from1970_GDP0 from1970_GDP1 from1970_totlsw{
		if "`window'" == "from1970" {
			local year 2000 
		}
		else {
			local year 1995
		}
		if "`window'" == "from1970_pauto95" {
			use ${final_dir}/regression_dataset_`window'.dta, clear
		}
		else {
			use ${final_dir}/regression_dataset_`window'_tfacit1.dta, clear
		}

		******************************************************
		clonevar LSW = lswMPm_1995_a
		clonevar HSW = hswMPm_1995_a
		clonevar VAEMP = vaempMPm_1995_a
		clonevar GDPGAP = lngdpgap_1995_a
		clonevar GDPPC = gdppcMPm_1995_a
		clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
		clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
		clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
		clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
		clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a
		clonevar spilloversown = spill${depvar}${ttt}_1995_a
		clonevar spilloversother = spillN${depvar}${ttt}_1995_a
		clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
		clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
		clonevar stockown = k${depvar}_${ttt} 
		clonevar stockownzero = k${depvar}_${ttt}0  
		clonevar stockother = kNOT_${depvar}_${ttt} 
		clonevar stockotherzero = kNOT_${depvar}_${ttt}0
		bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=`year'+2 & year <= 2009+2
		bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
		drop _total_${depvar}_${ttt}_1995
		egen yearctry = group(year country_shr_1995) if year <= 2009
		egen yearindustry = group(year industry) if year <= 2009
		egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
		sort lse_id year

		di "starting regressions for window `window'"
		*first column per window, domestic and foreign
		ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year >= `year' & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995 > 0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store c`window'_fd
		
		
		* second column per window, foreign
		ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=`year' & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
		estadd local f "\yes"
		estadd local iy "\yes"
		estadd local cy "\yes"
		estadd local has_stockspill "\yes"
		estadd local obs "{\num{`e(N)'}}"
		estadd local firms "{\num{`e(N_clust)'}}"
		estimates store c`window'_f

		* Save lsw var for corr
		mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
		keep if _m == 3
		keep if year >= 1995 & year <= 2009
		keep year BvD LSW 
		ren LSW LSW_`window'
		tempfile lswfile_`window'
		save `lswfile_`window'', replace

		di "Finished window `window'"
	}

	di "starting tables"

	*a version with the correct column order. noticed the issue in July 2024
	dhoztab cfrom1970_GDP0_fd cfrom1970_GDP0_f cfrom1970_GDP1_fd  cfrom1970_GDP1_f cfrom1970_totlsw_fd cfrom1970_totlsw_f using ${tab_dir}/appendix/Table_A39_alternative_weights_gdp.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label showdepvarinfo ///
	rawaddheader( ///
	Weight market size adj. &\multicolumn{2}{c}{GDP$^0$}&\multicolumn{2}{c}{GDP$^1$}&\multicolumn{2}{c}{$(w_L\cdot L)^{0.35}$} /// 
	\\ \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7} ///
	& {\makecell[c]{Dom. \\ and fgn.}} & {Fgn.} &{\makecell[c]{Dom. \\ and fgn.}} & {Fgn.} &{\makecell[c]{Dom. \\ and fgn.}} & {Fgn.} \\) ///
	drop(*zero _cons spill* stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) substitute(\_ _) ///
		depvar("Auto95") ///
		notes("This table varies the market size adjustment in the firm's country weights. Columns 1-2 do not adjust for GDP in the computation of the weights, Columns 3-4 use GDP instead of GDP$^{0.35}$ to adjust for country size and Columns 5--6 replace GDP with total low-skilled payment w$\_LL$ in the baseline formula. All regressions include firm, country-year  and industry-year fixed effects. In columns 2, 4, and 6 the macroeconmic variables are the normalized foreign variables as described in the text. Standard errors are clustered at the firm-level and reported in parentheses.")



	* Report: LSW correlations - not a table but we report a number.
	use BvD year lswMPm_1995_a using ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear 
	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(master)
	keep if _m == 3
	keep if year >= 1995 & year <= 2009
	rename lswMPm_1995_a LSW
	foreach window in f1970t1989 10yr from1970 from1970_pauto95 from1970_GDP0 from1970_GDP1 from1970_totlsw{
		mmerge BvD year using `lswfile_`window'', unmatched(master)
	}

	drop _m
	ren LSW_* _*
	estpost corr LSW _f1970t1989 _10yr _from1970 _from1970_pauto95 _from1970_GDP0 _from1970_GDP1 _from1970_totlsw, matrix
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store corr

	dhoztab corr using ${numb_dir}/Table_A39_alternative_weights_lsw_corr_numbers.tex, unstack m(8) replace noobs nopvalues nomtitles ///
		nomtitles nonumbers depvar(none) label cells(b(fmt(%10.3f)))  ///
		notes("Correlation of Low-Skill wage across the different weighting periods and methods; based on the baseline regression firms and time period 1995-2009. (Not in paper).")

    *again, } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl